package com.anbinh.doctor.fragment.benhnhan;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.HistoryMedicalAdapter;
import com.anbinh.doctor.model.DiseaseManifestations.Manifestations;
import com.anbinh.doctor.model.ResponseAppointment;
import com.anbinh.doctor.model.ResponseAppointment.Item;
import com.anbinh.doctor.model.ResponseAppointment.Schedules;
import com.anbinh.doctor.model.ResponsePatient.Patient;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.example.stickyheadergridview.GridItem;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentListAppoitMent extends Fragment {
	private static FragmentListAppoitMent fragment = null;
	private View mView;
	private CircleProgressBar mLoading;
	private ListView mListview;
	private UserLogin userLogin;

	private List<Manifestations> mList = null;
	private HistoryMedicalAdapter adapter;
	private int currentPage = 0;
	private View fotter;
	protected Manifestations currentBieuhien;
	protected List<Item> listGroup;

	public static FragmentListAppoitMent newInstance() {
		if (fragment == null) {
			fragment = new FragmentListAppoitMent();
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_list_images, null);
		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mListview = (ListView) mView.findViewById(R.id.listDrag);
		mListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				currentBieuhien = mList.get(position);
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, R.id.them_lich_su_kham);
				bundle.putBoolean(Utils.CUSTOMBAR, true);
				bundle.putBoolean(Utils.KEY_UPDATE, true);
				bundle.putInt(Utils.REQUEST_CODE, Utils.ONRESUME_BIEU_HIEN);
				bundle.putString(
						Utils.TITLE,
						getResources().getString(
								R.string.sua_lich_su_kham_chua_benh));
				Utils.getNewIntent(getActivity(), bundle);

			}

		});

		mLoading = (CircleProgressBar) mView.findViewById(R.id.loadding);
		onLoadData();

	}

	public Manifestations getCurrentBieuhien() {
		return currentBieuhien;
	}

	public void onLoadData() {
		Utils.onLoadding(mView, true);
		Patient patient = FragmentListPatients.newInstance()
				.getCurrentPatient();

		ServerController.getInstance(getActivity()).requestlistPatientsApp(
				userLogin.getData().getToken(),
				Utils.getCurrentDayName("yyyy-MM-dd"), patient.getId(),
				new OnRequest<ResponseAppointment>() {
					@Override
					public void onSuccess(ResponseAppointment data) {
						if (getActivity() == null)
							return;

						mLoading.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);

						if (data.getResponsecode() >= 0) {
							adapter = new HistoryMedicalAdapter(getActivity());
							listGroup = data.getData().getItems();
							if (listGroup == null || listGroup.size() == 0)
								return;

							for (int i = 0; i < listGroup.size(); i++) {
								GridItem item = new GridItem(listGroup.get(i)
										.getAddress(), "");
								adapter.addSeparatorItem(item);
								List<Schedules> mListItem = listGroup.get(i)
										.getListSchedules();
								if (mListItem == null || mListItem.size() == 0) {
									for (int j = 0; j < mListItem.size(); j++) {
										GridItem giGridItem = new GridItem(
												listGroup.get(i).getAddress(),
												"");
										giGridItem.setSchedules(mListItem
												.get(j));
										adapter.addItem(giGridItem);
									}
								}
								mListview.setAdapter(adapter);
							}

						}

					}

					@Override
					public void onError(int errorCode, String message) {
						mLoading.setVisibility(View.INVISIBLE);
						Utils.disableEnableControls(true, mView);
					}
				});
	}
}
